package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Account;

/**
 * DAO class for Account entity. This class contains all methods that
 * inserts/updates/deletes account info
 * 
 * @author nvu3
 *
 */
@Repository
public class AccountDAO {
	
	@PersistenceContext private EntityManager em;

	/**
	 * @return all accounts from Account table
	 */
	 @Transactional
	public List<Account> getAccounts() {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Account> query = em.createQuery("SELECT a FROM Account a",
				Account.class);
		List<Account> accounts = null;
		accounts = query.getResultList();
		return accounts;
	}
	 @Transactional
	 public void mergeAccount(Account account){
		 em.merge(account);
	 }

	/**
	 * Add account to Account table
	 * 
	 * @param account
	 *            Account entity that contains mapped with each column in
	 *            Account table
	 * @return
	 */
	 @Transactional
	public boolean addAccount(Account account) {
//		EntityManager em = TransactionManager.getEntityManager();

		// Check if account already in db
		try {
			if (isAccountExist(account.getAccountNumber()) == false) {
				em.persist(account);
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	
	/**
	 * Check if there is an existing account with the accountName
	 */
	 @Transactional
	private boolean isAccountExist(String accountNumber) {
		if (getAccountByAccNumber(accountNumber) != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get all accounts of a customer based on its customer Login ID
	 * @param custLoginId: the loginid of the customer
	 * @return
	 */
	 @Transactional
	public List<Account> getAccountsByCustomerId(String custLoginId) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Account> query = null;
		try {
			query = em.createQuery("SELECT a FROM Account a WHERE custId = ?",
					Account.class).setParameter(1, custLoginId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Remember to check null later
		return query.getResultList();
	}

	/**
	 * Get an account by the its unique accountNumber
	 */
	 @Transactional
	public Account getAccountByAccNumber(String accountNumber) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Account> query = null;
		try {
			query = em.createQuery(
					"SELECT a FROM Account a WHERE accountNumber = :accNum",
					Account.class).setParameter("accNum", accountNumber);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Remember to check null later
		List<Account> accounts = query.getResultList();
		if (!accounts.isEmpty()) {
			return accounts.get(0);
		}
		return null;
	}
	
	/**
	 * Get an account by the its id
	 */
	 @Transactional
	public Account getAccountByAccId(int accId) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Account> query = null;
		try {
			query = em.createQuery(
					"SELECT a FROM Account a WHERE number = :id",
					Account.class).setParameter("id", accId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Remember to check null later
		List<Account> accounts = query.getResultList();
		if (!accounts.isEmpty()) {
			return accounts.get(0);
		}
		return null;
	}
}
